package biz.noip.strategy.activities;

import java.util.HashMap;
import java.util.Map;

import org.andengine.engine.camera.SmoothCamera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.IEntity;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.util.FPSLogger;
import org.andengine.extension.tmx.TMXLoader;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TiledTextureRegion;
import org.andengine.ui.activity.SimpleBaseGameActivity;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.KeyEvent;
import android.view.WindowManager;
import android.widget.Toast;
import biz.noip.strategy.mediator.MediatorGame;
import biz.noip.strategy.scenes.GameScene;

public class GameActivity extends SimpleBaseGameActivity {

	protected GameScene mScene;
	protected static Context context;
	
	public static int CAMERA_WIDTH;
	public static int CAMERA_HEIGHT;

	public SmoothCamera mCamera;
	private BitmapTextureAtlas mBitmapTextureAtlas;
	private TMXLoader mTmxLoader;
	private Map<String, TiledTextureRegion> mTextureMap;
	
	private Font mFont = null;

	private static Handler handler;

	
	
	@SuppressWarnings("deprecation")
	public EngineOptions onCreateEngineOptions() {
		WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
        Display display = windowManager.getDefaultDisplay();
        CAMERA_HEIGHT = display.getHeight();
        CAMERA_WIDTH = display.getWidth();
		handler = new Handler();
		context = getApplicationContext();
		mTextureMap = new HashMap<String, TiledTextureRegion>();
		this.setCamera(new SmoothCamera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT, 1500, 1000, 2));
		MediatorGame.getInstance().setActivity(this);
		EngineOptions options = new EngineOptions(true, ScreenOrientation.PORTRAIT_FIXED, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT) , getCamera());
		return options;
	}

	@Override
	protected void onCreateResources() {
		this.mBitmapTextureAtlas = new BitmapTextureAtlas(this.getTextureManager(), CAMERA_WIDTH, CAMERA_HEIGHT, TextureOptions.DEFAULT);
		this.mTextureMap.put("mage_s", BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this.getApplicationContext(), "gfx/mage_s.png", 168, 0, 3, 1));
		this.mTextureMap.put("mage_f", BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this.getApplicationContext(), "gfx/mage_f.png", 168, 64, 3, 1));
		this.mTextureMap.put("fireball", BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, getApplicationContext(), "gfx/fireball.png", 0, 128, 4, 1));
		this.mTextureMap.put("warrior", BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this.getApplicationContext(), "gfx/warrior.png", 0, 0, 3, 4));
		this.mTextureMap.put("warrior_red", BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this.getApplicationContext(), "gfx/warrior_red.png", 360, 0, 3, 4));
		this.mTextureMap.put("board", BitmapTextureAtlasTextureRegionFactory.createTiledFromAsset(this.mBitmapTextureAtlas, this.getApplicationContext(), "gfx/board.png", 0, 192, 1, 1));
		this.mFont = FontFactory.create(this.getFontManager(), this.getTextureManager(), 256, 256, Typeface.create(Typeface.DEFAULT, Typeface.BOLD), 32);
		this.mEngine.getTextureManager().loadTexture(this.mBitmapTextureAtlas);
		this.mBitmapTextureAtlas.load();
		this.mFont.load();
	}

	@Override
	protected Scene onCreateScene() {
		this.mEngine.registerUpdateHandler(new FPSLogger());
		this.mTmxLoader = new TMXLoader(this.getAssets(), this.mEngine.getTextureManager(), TextureOptions.BILINEAR_PREMULTIPLYALPHA, this.getVertexBufferObjectManager());
		this.mScene = new GameScene(this.mTextureMap, this.mFont, this.getVertexBufferObjectManager(), this.mTmxLoader, this.getCamera());
		return mScene;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_BACK){
			finish();
		}
		if(keyCode == KeyEvent.KEYCODE_MENU){
			mScene.getGame().endGame();
		}
		return super.onKeyDown(keyCode, event); 
	}
	
	public void toast(final String str) {
		handler.post(new Thread(new Runnable() {
			public void run() {
				Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
			}
		}));
	}
	
	public void deleteChild(final IEntity entity, final Scene scene){
		this.runOnUpdateThread(new Runnable() {
			public void run() {
				scene.detachChild(entity);
			}
		});
	}

	public SmoothCamera getCamera() {
		return mCamera;
	}

	public void setCamera(SmoothCamera mCamera) {
		this.mCamera = mCamera;
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}
}

